<?php
/**
 * 1.修改订单状态
 * 2.给上级用户返佣
 * 3.给省代 市代 县代 返佣
 */

namespace app\api\service\order;


use app\api\model\Assistant;
use app\api\model\AssistantMoney;
use app\api\model\City;
use app\api\model\CityPartner;
use app\api\model\CityPartnerMoney;
use app\api\model\CountyPartner;
use app\api\model\CountyPartnerMoney;
use app\api\model\HotelRoom;
use app\api\model\HotelRoomOrder;
use app\api\model\ProvincePartner;
use app\api\model\ProvincePartnerMoney;
use app\api\model\System;
use app\api\model\User;
use app\api\model\UserInvitation;
use app\api\model\UserInvitationMoney;
use app\api\service\Token;
use think\Db;
use think\Exception;
use think\facade\Log;

class HotelRoomSer extends Token
{
    // 订单信息
    private $order;

    // 房间信息
    private $hotelRoom;

    function __construct($order)
    {
        $this->order = $order;
    }

    // 入口文件
    public function changeStatus()
    {
        if($this->order['status'] == 1) {
            Db::startTrans();
            try
            {
                $this->changeCommission();
                Db::commit();
                return true;
            }
            catch (Exception $ex)
            {
                Db::rollback();
                throw $ex;
                return false;
            }
        } else {
            return true;
        }
    }

    // 返佣操作
    private function changeCommission()
    {
        $HotelRoomOrder = new HotelRoomOrder();
        $HotelRoomOrder->save([
            'status' => 2,
            'pay_time' => time(),
        ],[ 'id' => $this->order['id'] ]);
//        $this->hotelRoom = HotelRoom::get($this->order['hotel_room_id']);
//        $this->userCommission();
//        $this->provinceCommission();
//        $this->cityCommission();
//        $this->countyCommission();
    }

    // 用户返佣操作
    private function userCommission()
    {
        $invitation = UserInvitation::where('invited_person_id',$this->order['user_id'])->find();
        $system = System::get(1);
        if($invitation) {
            if($system['invite_hotel_discount'] > 0) {
                $userInfo = User::get($invitation['inviter_id']);
                if($userInfo) {
                    $invite_money = $userInfo['invite_money'] + round($this->order['price']*$system['invite_hotel_discount'],2);
                    $userInfo->invite_money = $invite_money;
                    $userInfo->save();
                    UserInvitationMoney::create([
                        'user_id' => $userInfo['id'],
                        'invite_id' => $this->order['user_id'],
                        'type' => 1,
                        'money' => round($this->order['price']*$system['invite_hotel_discount'],2),
                        'user_invite_money' => $userInfo['invite_money'],
                        'order_no' => $this->order['order_no'],
                    ]);
                }
            }
        }
    }
    
    // 省代返佣操作
    private function provinceCommission()
    {
        $ProvincePartner = ProvincePartner::where('province_id',$this->hotelRoom['province_id'])
            ->where('status',2)
            ->find();
        if($ProvincePartner) {
            if($ProvincePartner['commission'] > 0) {
                $invite_money = $ProvincePartner['money'] + round($ProvincePartner['invite_money']/100 * $ProvincePartner['commission'],2);
                $ProvincePartner->money = $invite_money;
                $ProvincePartner->save();
                if(round($ProvincePartner['invite_money']/100 * $ProvincePartner['commission'],2) > 0) {
                    ProvincePartnerMoney::create([
                        'province_partner_id' => $ProvincePartner['id'],
                        'invite_id' => $this->order['user_id'],
                        'money' => round($ProvincePartner['invite_money']/100 * $ProvincePartner['commission'],2),
                        'order_no' => $this->order['order_no'],
                        'province_partner_money' => $ProvincePartner['money']
                    ]);
                }
            }
            $list = Assistant::where('pid',$ProvincePartner['id'])
                ->where('status',1)
                ->select()->toArray();
            foreach ($list as $k=>$v) {
               $this->assistantCommission($v,$ProvincePartner);
            }
        }
    }
    
    // 市代返佣操作
    private function cityCommission()
    {
        $CityPartner = CityPartner::where('city_id',$this->hotelRoom['city_id'])
            ->where('status',2)
            ->find();
        if($CityPartner) {
            if($CityPartner['commission'] > 0) {
                $invite_money = $CityPartner['money'] + round($this->order['price'] * $CityPartner['commission'],2);
                $CityPartner->money = $invite_money;
                $CityPartner->save();
                if(round($this->order['price'] * $CityPartner['commission'],2) > 0) {
                    CityPartnerMoney::create([
                        'city_partner_id' => $CityPartner['id'],
                        'invite_id' => $this->order['user_id'],
                        'money' => round($this->order['price'] * $CityPartner['commission'],2),
                        'order_no' => $this->order['order_no'],
                        'city_partner_money' => $CityPartner['money']
                    ]);
                }
            }
            $list = Assistant::where('pid',$CityPartner['id'])
                ->where('status',2)
                ->select()->toArray();
            foreach ($list as $k=>$v) {
                $this->assistantCommission($v,$CityPartner);
            }
        }
    }

    // 县代返佣操作
    private function countyCommission()
    {
        $CountyPartner = CountyPartner::where('county_id',$this->hotelRoom['county_id'])
            ->where('status',2)
            ->find();
        if($CountyPartner) {
            if($CountyPartner['commission'] > 0) {
                $invite_money = $CountyPartner['money'] + round($this->order['price'] * $CountyPartner['commission'],2);
                $CountyPartner->money = $invite_money;
                $CountyPartner->save();
                if(round($this->order['price'] * $CountyPartner['commission'],2) > 0) {
                    CountyPartnerMoney::create([
                        'invite_id' => $this->order['user_id'],
                        'county_partner_id' => $CountyPartner['id'],
                        'money' => round($this->order['price'] * $CountyPartner['commission'],2),
                        'order_no' => $this->order['order_no'],
                        'county_partner_money' => $CountyPartner['money']
                    ]);
                }
            }
            $list = Assistant::where('pid',$CountyPartner['id'])
                ->where('status',3)
                ->select()->toArray();
            foreach ($list as $k=>$v) {
                $this->assistantCommission($v,$CountyPartner);
            }
        }

    }

    // 助理返佣
    private function assistantCommission($info,$partner)
    {
        if($info) {
            if($partner['invite_money'] > 0) {
                $money = $info['money'] + round($partner['invite_money']/100 * $partner['assistant_commission'],2);
                Assistant::where('id',$info['id'])->update([
                    'money' => $money
                ]);
                AssistantMoney::create([
                    'invite_id' => $this->order['user_id'],
                    'assistant_id' => $info['id'],
                    'money' => round($partner['invite_money']/100 * $partner['assistant_commission'],2),
                    'order_no' => $this->order['order_no'],
                    'assistant_money' => $info['money']
                ]);
            }
        }
    }
}